package com.health.httpbean.upload;

import android.text.TextUtils;
import android.util.Log;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.Utils;
import com.health.httpbean.HttpNewConstant;
import com.health.httpbean.HttpResultBean;
import com.health.httpbean.model.BpModel;
import com.health.httpbean.model.EcgModel;
import com.health.httpbean.model.MeasureBean;
import com.health.httpbean.model.PoModel;
import com.health.httpbean.model.UeModel;
import com.health.tablebean.SBCL_DATA_NEW;
import com.health.tools.GlobalConts;
import com.health.viewscontrols.NovaApplication;
import com.novabluetooth.views.twj.PwBean;

import java.io.File;
import java.util.List;

import rxhttp.RxHttp;

/**
 * author : xiaoxin
 * date   : 2020/12/19 19:02
 * desc   :
 * update :
 */
public class SbclDataUpload {

    private static final String TAG = "SbclDataUpload";

    /**
     * 查询未上传档案数据
     * @return
     */
    public static List<SBCL_DATA_NEW> searchDbData() {
        List<SBCL_DATA_NEW> list = NovaApplication.fdbmanger.findAllByWhere(SBCL_DATA_NEW.class,
                "IS_UPDATE = 'Y' OR IS_UPDATE IS NULL");
        return list;
    }

    public static void uploadSbclDataNew() {
        try {
            if (!GlobalConts.isNetworkAvailable(Utils.getApp())) {
                // showTextDialog("请先检查网络");
                return;
            }
            List<SBCL_DATA_NEW> baseinfos = searchDbData();
            if (baseinfos == null || baseinfos.size() <= 0) {
                return;
            }
            for (int i = 0; i < baseinfos.size(); i++) {
                requestSbclDataNew(baseinfos.get(i));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void requestSbclDataNew(SBCL_DATA_NEW sbclDataNew) {
        try {
            String json = "";
            String url = "";
            if ("AH".equalsIgnoreCase(sbclDataNew.getCL_TYPE())) {
                MeasureBean measureBean = new MeasureBean();
                measureBean.setTemperature(sbclDataNew.getCL_VALUE());
                measureBean.setIdCard(sbclDataNew.getIDCARD());
                measureBean.setPhone(sbclDataNew.getIDCARD());
                measureBean.setTestTime(sbclDataNew.getCL_TIME());
                measureBean.setDeviceSim(GlobalConts.generateIMEI(Utils.getApp()));
                json = GsonUtils.toJson(measureBean);
                url = HttpNewConstant.savePersonnelTiwen;
            } else if ("BF".equalsIgnoreCase(sbclDataNew.getCL_TYPE())) {
                MeasureBean measureBean = new MeasureBean();
                PwBean pwBean = GsonUtils.fromJson(sbclDataNew.getCL_VALUE(), PwBean.class);
                measureBean.setHdl(pwBean.getHdl() + "");
                measureBean.setLdl(pwBean.getLdl() + "");
                measureBean.setTrig(pwBean.getTg() + "");
                measureBean.setCholesterol(pwBean.getTc() + "");
                measureBean.setIdCard(sbclDataNew.getIDCARD());
                measureBean.setPhone(sbclDataNew.getIDCARD());
                measureBean.setTestTime(sbclDataNew.getCL_TIME());
                measureBean.setDeviceSim(GlobalConts.generateIMEI(Utils.getApp()));
                json = GsonUtils.toJson(measureBean);
                url = HttpNewConstant.saveK5BloodfatFour;
            } else if ("BG".equalsIgnoreCase(sbclDataNew.getCL_TYPE())) {
                MeasureBean measureBean = new MeasureBean();
                measureBean.setBloodSugar(sbclDataNew.getCL_VALUE());
                measureBean.setIdCard(sbclDataNew.getIDCARD());
                measureBean.setPhone(sbclDataNew.getIDCARD());
                measureBean.setTestTime(sbclDataNew.getCL_TIME());
                measureBean.setDeviceSim(GlobalConts.generateIMEI(Utils.getApp()));
                json = GsonUtils.toJson(measureBean);
                url = HttpNewConstant.savePersonnelXuetang;
            } else if ("BO".equalsIgnoreCase(sbclDataNew.getCL_TYPE())) {
                PoModel poModel = GsonUtils.fromJson(sbclDataNew.getCL_VALUE(), PoModel.class);
                MeasureBean measureBean = new MeasureBean();
                measureBean.setBloodOxygenSaturation(poModel.getBloodOxygenSaturation());
                measureBean.setHeartRate(poModel.getHeartRate());
                measureBean.setIdCard(sbclDataNew.getIDCARD());
                measureBean.setPhone(sbclDataNew.getIDCARD());
                measureBean.setTestTime(sbclDataNew.getCL_TIME());
                measureBean.setDeviceSim(GlobalConts.generateIMEI(Utils.getApp()));
                json = GsonUtils.toJson(measureBean);
                url = HttpNewConstant.savePersonnelXueyang;
            } else if ("BP".equalsIgnoreCase(sbclDataNew.getCL_TYPE())) {
                BpModel bpModel = GsonUtils.fromJson(sbclDataNew.getCL_VALUE(), BpModel.class);
                MeasureBean measureBean = new MeasureBean();
                measureBean.setHighpressure(bpModel.getHighpressure());
                measureBean.setLowvoltage(bpModel.getLowvoltage());
                measureBean.setPulse(bpModel.getPulse());
                measureBean.setIdCard(sbclDataNew.getIDCARD());
                measureBean.setPhone(sbclDataNew.getIDCARD());
                measureBean.setTestTime(sbclDataNew.getCL_TIME());
                measureBean.setDeviceSim(GlobalConts.generateIMEI(Utils.getApp()));
                json = GsonUtils.toJson(measureBean);
                url = HttpNewConstant.savePersonnelXueya;
            } else if ("ECG".equalsIgnoreCase(sbclDataNew.getCL_TYPE())) {
    //            EcgModel ecgModel = GsonUtils.fromJson(sbclDataNew.getCL_VALUE(), EcgModel.class);
    //            MeasureBean measureBean = new MeasureBean();
    //            measureBean.setIdCard(sbclDataNew.getIDCARD());
    //            measureBean.setHr(ecgModel.getHr());
    //            measureBean.setEcgResult(ecgModel.getEcgResult());
    //            measureBean.setEcgImg(ecgModel.getEcgImg());
    //            json = GsonUtils.toJson(measureBean);
    //            url = "api/health/personnelxindian/savePersonnelXindian";
            } else if ("UE".equalsIgnoreCase(sbclDataNew.getCL_TYPE())) {
                UeModel ueModel = GsonUtils.fromJson(sbclDataNew.getCL_VALUE(), UeModel.class);
                MeasureBean measureBean = new MeasureBean();
                measureBean.setUro(ueModel.getUro().replaceAll(">","").replaceAll("<",""));
                measureBean.setBld(ueModel.getBld().replaceAll(">","").replaceAll("<",""));
                measureBean.setBil(ueModel.getBil().replaceAll(">","").replaceAll("<",""));
                measureBean.setKet(ueModel.getKet().replaceAll(">","").replaceAll("<",""));
                measureBean.setGlu(ueModel.getGlu().replaceAll(">","").replaceAll("<",""));
                measureBean.setPro(ueModel.getPro().replaceAll(">","").replaceAll("<",""));
                measureBean.setPh(ueModel.getPh().replaceAll(">","").replaceAll("<",""));
                measureBean.setNit(ueModel.getNit().replaceAll(">","").replaceAll("<",""));
                measureBean.setLeu(ueModel.getLeu().replaceAll(">","").replaceAll("<",""));
                measureBean.setSg(ueModel.getSg().replaceAll(">","").replaceAll("<",""));
                measureBean.setVc(ueModel.getVc().replaceAll(">","").replaceAll("<",""));
                measureBean.setIdCard(sbclDataNew.getIDCARD());
                measureBean.setPhone(sbclDataNew.getIDCARD());
                measureBean.setTestTime(sbclDataNew.getCL_TIME());
                measureBean.setDeviceSim(GlobalConts.generateIMEI(Utils.getApp()));
                json = GsonUtils.toJson(measureBean);
                url = HttpNewConstant.saveTraPersonnelNiaoye;
            } else if ("NS".equalsIgnoreCase(sbclDataNew.getCL_TYPE())) {
                MeasureBean measureBean = new MeasureBean();
                measureBean.setUricAcid(sbclDataNew.getCL_VALUE());
                measureBean.setIdCard(sbclDataNew.getIDCARD());
                measureBean.setPhone(sbclDataNew.getIDCARD());
                measureBean.setTestTime(sbclDataNew.getCL_TIME());
                measureBean.setDeviceSim(GlobalConts.generateIMEI(Utils.getApp()));
                json = GsonUtils.toJson(measureBean);
                url = HttpNewConstant.saveTraPersonnelUricacid;
            } else if ("DGC".equalsIgnoreCase(sbclDataNew.getCL_TYPE())) {
                MeasureBean measureBean = new MeasureBean();
                measureBean.setCholesterol(sbclDataNew.getCL_VALUE());
                measureBean.setIdCard(sbclDataNew.getIDCARD());
                measureBean.setPhone(sbclDataNew.getIDCARD());
                measureBean.setTestTime(sbclDataNew.getCL_TIME());
                measureBean.setDeviceSim(GlobalConts.generateIMEI(Utils.getApp()));
                json = GsonUtils.toJson(measureBean);
                url = HttpNewConstant.trapersonnelcholesterol;
            } else if ("XHDB".equalsIgnoreCase(sbclDataNew.getCL_TYPE())) {
                MeasureBean measureBean = new MeasureBean();
                measureBean.setHemogloin(sbclDataNew.getCL_VALUE());
                measureBean.setIdCard(sbclDataNew.getIDCARD());
                measureBean.setPhone(sbclDataNew.getIDCARD());
                measureBean.setTestTime(sbclDataNew.getCL_TIME());
                measureBean.setDeviceSim(GlobalConts.generateIMEI(Utils.getApp()));
                json = GsonUtils.toJson(measureBean);
                url = HttpNewConstant.saveK5Hemoglobin;
            } else if ("VISION".equalsIgnoreCase(sbclDataNew.getCL_TYPE())) {
//                VisionBean visionBean = GsonUtils.fromJson(sbclDataNew.getCL_VALUE(), VisionBean.class);
//                MeasureBean measureBean = new MeasureBean();
//                measureBean.setEleft(visionBean.getEleft());
//                measureBean.setEright(visionBean.getEright());
//                measureBean.setLleft(visionBean.getLleft());
//                measureBean.setLright(visionBean.getLright());
//                measureBean.setColor(visionBean.getColor());
//                measureBean.setQjsLeft(visionBean.getQjsLeft());
//                measureBean.setQjsRight(visionBean.getQjsRight());
//                measureBean.setZjcLeft(visionBean.getZjcLeft());
//                measureBean.setZjcRight(visionBean.getZjcRight());
//                measureBean.setZdaLeft(visionBean.getZdaLeft());
//                measureBean.setZdaRight(visionBean.getZdaRight());
//                measureBean.setIdCard(sbclDataNew.getIDCARD());
//                measureBean.setPhone(sbclDataNew.getIDCARD());
//                measureBean.setTestTime(sbclDataNew.getCL_TIME());
//                measureBean.setDeviceSim(GlobalConts.generateIMEI(Utils.getApp()));
//                json = GsonUtils.toJson(measureBean);
//                url = HttpNewConstant.saveTraPersonnelEye;
            } else if ("FM".equalsIgnoreCase(sbclDataNew.getCL_TYPE())) {
//                FmModel fmModel = GsonUtils.fromJson(sbclDataNew.getCL_VALUE(), FmModel.class);
//                MeasureBean measureBean = new MeasureBean();
//                measureBean.setWeight(fmModel.getWeight());
//                measureBean.setHeight(fmModel.getHeight());
//                measureBean.setBmi(fmModel.getBmi());
//                measureBean.setIdCard(sbclDataNew.getIDCARD());
//                measureBean.setPhone(sbclDataNew.getIDCARD());
//                measureBean.setTestTime(sbclDataNew.getCL_TIME());
//                measureBean.setDeviceSim(GlobalConts.generateIMEI(Utils.getApp()));
//                measureBean.setFp(fmModel.getFp());
//                measureBean.setTbwc(fmModel.getTbwc());
//                measureBean.setMm(fmModel.getMm());
//                measureBean.setSm(fmModel.getSm());
//                measureBean.setBmr(fmModel.getBmr());
//                measureBean.setVfal(fmModel.getVfal());
//                measureBean.setBodyAge(fmModel.getBodyAge());
//                measureBean.setProteinRate(fmModel.getProteinRate());
//                measureBean.setEwr(fmModel.getEwr());
//                measureBean.setSfr(fmModel.getSfr());
//                json = GsonUtils.toJson(measureBean);
//                url = HttpNewConstant.saveTraPersonnelShangheBf;
            }

            if ("ECG".equalsIgnoreCase(sbclDataNew.getCL_TYPE())) {
                EcgModel ecgModel = GsonUtils.fromJson(sbclDataNew.getCL_VALUE(), EcgModel.class);
                MeasureBean measureBean = new MeasureBean();
                measureBean.setIdCard(sbclDataNew.getIDCARD());
                measureBean.setPhone(sbclDataNew.getIDCARD());
                measureBean.setHr(ecgModel.getHr());
                measureBean.setEcgResult(ecgModel.getEcgResult());
                measureBean.setEcgImg(ecgModel.getEcgImg());
                measureBean.setTestTime(sbclDataNew.getCL_TIME());
                measureBean.setDeviceSim(GlobalConts.generateIMEI(Utils.getApp()));
                json = GsonUtils.toJson(measureBean);
                url = HttpNewConstant.savePersonnelXindian;
                if (TextUtils.isEmpty(ecgModel.getEcgImg())) {
                    requestHttpSbclDataNew(url, json, sbclDataNew);
                } else {
                    uploadFile(HttpNewConstant.saveEcgFile, json, measureBean, sbclDataNew);
                }
            } else {
                requestHttpSbclDataNew(url, json, sbclDataNew);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static void requestHttpSbclDataNew(String url, String json, SBCL_DATA_NEW sbclDataNew) {
        String baseUrl = HttpNewConstant.getHttpNewBaseUrl();
        RxHttp.postJson(baseUrl + url)
                .addAll(json)
                .asClass(HttpResultBean.class)
                .subscribe(resultBean -> {
                    LogUtils.e(resultBean + " --URL--" + url);
                    // 操作成功 代表数据上传成功，因为code值不靠谱
                    if (resultBean.isOk()) {
                        sbclDataNew.setIS_UPDATE("N");
                        NovaApplication.fdbmanger.updata(sbclDataNew);
                    } else {
                        LogUtils.file("体检数据上传失败(AndroidRegister):" + resultBean.toString());
                        Log.e(TAG, "upLoadHttp: " + resultBean.getResponseMsg());
                    }
                }, throwable -> {
                    LogUtils.file("上传出错了" + throwable.toString());
                });
    }

    // 上传心电图片
    public static void uploadFile(String url, String json,MeasureBean measureBean , SBCL_DATA_NEW sbclDataNew) {
        String baseUrl = HttpNewConstant.getHttpNewBaseUrl();
        RxHttp.postForm(baseUrl + url, json)
                .addFile("file", new File(measureBean.getEcgImg()))
                .asClass(HttpResultBean.class)
                .subscribe(resultBean -> {
                    LogUtils.e(resultBean + " --URL--" + url);
                    // 操作成功 代表数据上传成功，因为code值不靠谱
                    if (resultBean != null && resultBean.getFullImagePaths() != null) {
                        measureBean.setEcgImg(resultBean.getFullImagePaths());
                        requestHttpSbclDataNew(HttpNewConstant.savePersonnelXindian, GsonUtils.toJson(measureBean), sbclDataNew);
                    } else {
                        LogUtils.file("体检数据上传失败(AndroidRegister):" + resultBean.toString());
                        Log.e(TAG, "upLoadHttp: " + resultBean.getResponseMsg());
                    }
                }, throwable -> {
                    LogUtils.file("上传出错了" + throwable.toString());
                });

    }


//    public static void setTzData(MeasureBean measureBean, ZytzcsjgBean zytzcsjgBean) {
//        measureBean.setMild(zytzcsjgBean.getMild());
//        measureBean.setMildScore(zytzcsjgBean.getMildScore());
//        measureBean.setFaint(zytzcsjgBean.getFaint());
//        measureBean.setFaintScore(zytzcsjgBean.getFaintScore());
//        measureBean.setYang(zytzcsjgBean.getYang());
//        measureBean.setYangScore(zytzcsjgBean.getYangScore());
//        measureBean.setYin(zytzcsjgBean.getYin());
//        measureBean.setYinScore(zytzcsjgBean.getYinScore());
//        measureBean.setPhlegmdamp(zytzcsjgBean.getPhlegmdamp());
//        measureBean.setPhlegmdampScore(zytzcsjgBean.getPhlegmdampScore());
//        measureBean.setMuggy(zytzcsjgBean.getMuggy());
//        measureBean.setMuggyScore(zytzcsjgBean.getMuggyScore());
//        measureBean.setBloodstasis(zytzcsjgBean.getBloodstasis());
//        measureBean.setBloodstasisScore(zytzcsjgBean.getBloodstasisScore());
//        measureBean.setQiconstraint(zytzcsjgBean.getQiconstraint());
//        measureBean.setQiconstraintScore(zytzcsjgBean.getQiconstraintScore());
//        measureBean.setCharacteristic(zytzcsjgBean.getCharacteristic());
//        measureBean.setCharacteristicScore(zytzcsjgBean.getCharacteristicScore());
//        measureBean.setZytzcsBean(zytzcsjgBean.getZytzcsBean());
//        measureBean.setResult(zytzcsjgBean.getTzName());
//    }
}
